from BeautifulSoup import BeautifulSoup

def changeBrasilValueToEnglishValue(str):
    #Remove period and change comma to period
    while str.find(".") != -1:
        i = str.find(".")
        str=str[0:i]+str[i+1:]
    while str.find(",") != -1:
        i = str.find(",")
        str=str[0:i]+"."+str[i+1:]
    return str

def changeStrangeStringToFamiliar(str):
    strange = ["\xc2\xba","\xc3\xaa"]
    familiar = ["o","e"]
    i = 0
    for s in strange:
        while str.find(s) != -1:
            index = str.index(s)
            str = str[:index]+familiar[i]+str[index+len(s):]
        i+=1
    return str

def getLotery(name,file_name):
    #File management
    try:
        file = open(file_name,"r")
    except:
        "File " + file_name + " not found"
        return
    
    #Parser management
    soup = BeautifulSoup(file)
    table = soup.findAll("table")[0]
    table_lines = table.findAll("tr")
    first_line = table_lines[0]
    cols_title = first_line.findAll("th")
    
    Loteria = []
    titles = []
    i = 0
    for tr in table_lines:
        #Get title
        if i == 0:
            cols_title = tr.findAll("th")
            #Caso nao ha th's
            if len(cols_title) == 0:
                cols_title = tr.findAll("td")
            for th in cols_title:
                font_tag = th.findAll("font")[0]
                title=str(font_tag.contents[0])
                title=changeStrangeStringToFamiliar(title)
                titles.append(title)
                i+=1
            continue
        #Get sorteios
        line_content = tr.findAll("td")
        sorteio = {}
        n_column = len(line_content)
        for column in range(0,n_column):
            value = str(line_content[column].contents[0])
            if column == 1:
                sorteio[titles[column]] = value
            else: 
                value = changeBrasilValueToEnglishValue(value)
                #Just two decimals places
                try:
                    value = "%.2f" % float(value)
                except:
                    pass
                sorteio[titles[column]] = value
            print sorteio
            Loteria.append(sorteio)
    file = open(name,"w")
    file.write(str(Loteria))

names = ["duplacena","federal","lotofacil","lotogol","lotomania","megasena","quina","timemania"]
files =["./htmls/duplacena","./htmls/federal","./htmls/lotofacil.htm","./htmls/lotogol.htm","./htmls/lotomania.htm","./htmls/megasena.htm","./htmls/quina.htm","./htmls/timemania.htm"] 
n_names = len(names)
for i in range(0,n_names):
    getLotery(names[i],files[i])
